﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Agnes.Core;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// DocsLibSmartTree.xaml 的交互逻辑
    /// </summary>
    public partial class DocsLibSmartTreeView : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DocsLibSmartTreeView()
        {
            InitializeComponent();

            AgnesServiceManager.AgnesSlnService.SlnClosedHandler += new EventHandler(AgnesSlnService_SlnClosedHandler);
        }

        void AgnesSlnService_SlnClosedHandler(object sender, EventArgs e)
        {
            lbArticles.ItemsSource = null;
        }

        private void StackPanel_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {           
            //StackPanel tb = sender as StackPanel;
            //if (tvMyDocsTree.SelectedItem != null)
            //{
            //    var selNode = tvMyDocsTree.SelectedItem;

            //    if (selNode is IArticleNode)
            //    {
            //        MyDocsLibManager.Instance.ArticleManager.SelectedNode = (IArticleNode) selNode;
            //    }

            //    if (e.ClickCount == 2 && e.ChangedButton == MouseButton.Left)
            //    {
            //        TreeViewItem treeViewItem 
            //            = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            //        if (treeViewItem != null)
            //        {
            //            treeViewItem.IsExpanded = !treeViewItem.IsExpanded;
            //        }
            //    }
            //}
        }

        private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
           
            if (treeViewItem != null)
            {
                treeViewItem.IsSelected = true;
                treeViewItem.Focus();
                MyDocsLibManager.Instance.ArticleManager.SelectedNode = treeViewItem.DataContext as IArticleNode;
                e.Handled = true;
            }
        }


        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
            {
                source = VisualTreeHelper.GetParent(source);
            }

            return source;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lbArticles.ItemsSource = MyDocsLibManager.Instance.ArticleManager.GetArticels(cbLabels.SelectedItem as string);
        }

        private void OpenArticle(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                TextBlock tb = sender as TextBlock;
                Article article = tb.DataContext as Article;

                try
                {
                    IDocumentPanel documentPanel = DocumentPanelsHelper.GetDocumentByModel(article);
                    article.PreviewCount += 1;
                    if (documentPanel != null)
                    {
                        AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel = documentPanel;
                        ((documentPanel.Content as ArticleView).DataContext as ArticleViewModel).IsEditMode = false;
                    }
                    else
                    {
                        ArticleViewModel vm = new ArticleViewModel(article);
                        AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.Add(vm);
                    }
                }
                catch (Exception ex)
                {
                    AgnesServiceManager.LogServ.Error("MyDocsLib--预览文章失败，错误：" + ex.Message);
                }

            }
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel tb = sender as StackPanel;
            if (tvMyDocsTree.SelectedItem != null)
            {
                var selNode = tb.DataContext;

                if (selNode is IArticleNode)
                {
                    MyDocsLibManager.Instance.ArticleManager.SelectedNode = (IArticleNode)selNode;
                }

                if (e.ClickCount == 2)
                {
                    TreeViewItem treeViewItem
                        = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
                    if (treeViewItem != null)
                    {
                        treeViewItem.IsExpanded = !treeViewItem.IsExpanded;
                    }
                }
            }
        } 
    }
}
